package edu.cuit.jead.mynetdisk.exception;

import edu.cuit.jead.mynetdisk.common.Code;
import edu.cuit.jead.mynetdisk.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public <T> Result<T> exceptionHandler(Exception e){
        //这里先判断拦截到的Exception是不是我们自定义的异常类型
        if(e instanceof AppException){
            AppException appException = (AppException)e;
            return Result.error(appException.getCode(),appException.getMsg());
        }
        e.printStackTrace();
        //如果拦截的异常不是我们自定义的异常(例如：数据库主键冲突)
        return Result.error(500,"服务器端异常");
    }
}
